from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.response import Response
from Util.Return import get_next_available_name
from Util.SQL import *


@csrf_exempt
@api_view(['GET', 'POST'])
def get_algorithm(request):
    sql_string = "SELECT * FROM algorithm;"
    algorithm = select(sql_string)
    return Response(data=algorithm)


@csrf_exempt
@api_view(['GET', 'POST'])
def change_algorithm(request):
    data_dict = request.data
    update_data = {"name": data_dict['name'], "type": data_dict['type'],
                   "attribute": data_dict['attribute'], "available": data_dict['available']}
    condition = f"algorithm_id = '{data_dict['algorithmId']}'"
    status = update(update_data, condition, "algorithm")
    return Response(data=status)


@csrf_exempt
@api_view(['GET', 'POST'])
def del_algorithm(request):
    data_dict = request.data
    condition = f"algorithm_id = '{data_dict['algorithmId']}'"
    table = "algorithm"
    result = delete(condition, table)
    return Response(data=result)


@csrf_exempt
@api_view(['GET', 'POST'])
def copy_algorithm(request):
    data_dict = request.data
    result = insert(data_dict, "algorithm")
    return Response(data=result)
